package com.citicpub_cloud.restfull.security;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.citicpub_cloud.restfull.common.constant.AdminUserConstant;
import com.citicpub_cloud.restfull.utils.SessionUtils;

/**
 * Created by dlm on 2016/4/18.
 */
public class SecurityUtils {
    /**
     * @desccroption:获取用户登录session key
     * @date: 2016年9月8日 上午9:46:53
     * @author:zhangyufeng@citicpub.com
     * @param request
     * @return
     * String
     */
    public static String getKeyFromRequestHeader(HttpServletRequest request){
        String uid = request.getHeader("uid");
        String accToken = request.getHeader("acc-token");
        if(StringUtils.isBlank(uid) || StringUtils.isBlank(accToken)){
        	Cookie[] cookieList  = request.getCookies();
        	if(null != cookieList){
        		 for(Cookie c : cookieList){
                 	System.out.println(c.getName());
                 	if(c.getName().equals("uid")){
                 		uid = c.getValue();
                 	}
                 	if(c.getName().equals("acc-token")){
                 		accToken = c.getValue();
                 	}
                 }
        	}         
        }
        
        if(StringUtils.isNotBlank(uid) && StringUtils.isNotBlank(accToken)){
            String key = SessionUtils.getRedisSessionKeyForAdmin(AdminUserConstant.SessionConstant.REDIS_SESSION_TAG_NAME,uid,accToken);
            return key;
        }
        return null;
    }
}
